/************************************************************************************/
/*  Program name CAPFNDIMP_RESET_PWD.sql                                            */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID			VARCHAR2(100)	:=	'CAPFNDIMP_RESET_PWD';

	lv_Scc_Count		NUMBER			:=	0;
	lv_Err_Count		NUMBER			:=	0;
	lv_Err_Msg			VARCHAR2(500)	:=	'No Error';

	lv_FileName			VARCHAR2(100)	:=	'CAPFND_RESET_PWD.txt';
	lv_PathName			VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File			UTL_FILE.File_Type;

	-- Variables
	lv_Random_Number	BINARY_INTEGER;
	lv_Init_Password	VARCHAR2(100);
	lv_User_Name		VARCHAR2(100);

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_USER_MAP;

BEGIN

	lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
	FOR C1 IN Cur_F LOOP
		BEGIN
			lv_User_Name		:=	NULL;
			lv_Random_Number	:=	NULL;
			lv_Init_Password	:=	NULL;
		
			SELECT	User_Name
			INTO	lv_User_Name
			FROM	FND_USER
			WHERE	User_ID	=	C1.USER_ID_N;

			DBMS_RANDOM.Initialize(24393);
			DBMS_RANDOM.Seed(C1.USER_ID_N);
			lv_Random_Number	:=	DBMS_RANDOM.Random;
			DBMS_RANDOM.Terminate;

			lv_Init_Password	:=	SUBSTR(TO_CHAR(ABS(lv_Random_Number)),1,6);

			FND_USER_PKG.UpdateUser(
			x_user_name					=>	lv_User_Name,
			x_owner						=>	'SEED',
			x_unencrypted_password		=>	lv_Init_Password
			);

			-- Output Data
			UTL_FILE.PUT_LINE(lv_UTL_File, lv_User_Name||CHR(9)||lv_Init_Password);

		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	UTL_FILE.FClose(lv_UTL_File);
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
